
#
# write data with compress and base64-encode
#  

import	base64
import	zlib
import	time
import	fcntl

def	test_write ( filename )	:
	key_prefix = 'abcdefghijklmnopqrstuvwxyz'

	records_count = 100000
	time_start = time.time()
	fh = open ( filename, 'a+' )
	fcntl.lockf( fh, fcntl.LOCK_EX )
	for i in xrange(records_count) :
		key = "%s%d" % (key_prefix, i)
		value = base64.standard_b64encode( zlib.compress( key ) )
		fh.write ( "%s\t%s\n" % (key, value) )
	fcntl.lockf( fh, fcntl.LOCK_UN )
	fh.close()
	time_stop = time.time()
	time_last = time_stop - time_start
	tps = records_count / time_last
	print "records-count=%d time-last=%.3f seconds tps=%.3f" % ( records_count, time_last, tps )

if	__name__ == '__main__'	:
	filename = 'base64.dat'
	test_write ( filename )

